Skip to content

fix(client): select oneOf variant by JSON kind when validation is off - #138

Open
jd-lara wants to merge 1 commit into
JuliaComputing:mainfrom
QXT-Energy:jd/oneof-decode-no-validate
Open

jd-lara wants to merge 1 commit into
JuliaComputing:mainfrom
QXT-Energy:jd/oneof-decode-no-validate

Conversation

@jd-lara

@jd-lara jd-lara commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Problem

A generated oneOf decoder can throw when validation is off.
The decoder tries every variant and counts the ones that convert.
Julia converts the numbers 1.0 and 1 to Bool.
So a number matches both a number variant and a boolean variant.
The decoder then throws "oneOf value did not select exactly one variant".
Decoding with validation on works, because schema checks pick one branch.

Reproduction

Schema:

oneOf:
  - const: "NaN"
  - type: number
  - type: boolean

The generator emits a wrapper with value::Union{Bool,Float64,String}.
Call the generated decoder with validate = false and the value 1.0.
The call throws a DecodeError.
The value 1 fails in the same way.

Cause

_decode(::Type{Bool}, value) has no specific method for non-boolean input.
The call falls to the generic method, which calls convert(Bool, value).
That call succeeds for 0, 1, 0.0, and 1.0.

Fix

Add two _decode methods for Bool in src/runtime.jl.
They throw DecodeError unless the input is a JSON boolean.
The second method takes Integer.
It prevents an ambiguity with the existing T<:Integer method.
The validating path does not change.

Tests

The new test in test/models.jl generates a client from an inline spec.
It decodes 1.0, 1, true, and "NaN" with validation on and off.
Each input selects exactly one variant with the correct value and type.
The test fails before the fix and passes after it.
The full suite passes.

With validate = false, decoding tries every oneOf variant and counts
the ones that convert. A JSON number such as 1.0 or 1 converted to
Bool, so it matched both Float64 and Bool and decode threw.

Make _decode(Bool, x) accept only a JSON boolean.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant